/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package routing;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Extends the ArrayList<RouteNodeAbstraction> collection
 * @author artem
 */
public class RouteNodeList extends ArrayList<RouteNodeAbstraction> {

	/**
	 * Constructs an ArrayList<RouteNodeAbstraction> with initial capacity 10
	 */
	public RouteNodeList() {
		super();
	}

	/**
	 * Constructs an ArrayList<RouteNodeAbstraction> containing the elements of the specified collection, in the order they
	 * are returned by the collection's iterator
	 * @param rnl The collection whose elements are to be placed into the list
	 */
	public RouteNodeList(Collection<RouteNodeAbstraction> rnl) {
		super(rnl);
	}

	/**
	 * Constructs an empty ArrayList<RouteNodeAbstraction> with the specified initial capacity
	 * @param initialCapacity
	 */
	public RouteNodeList(int initialCapacity) {
		super(initialCapacity);
	}

	/**
	 * Updates the RouteNodeList by adding a new child element to the root (direct recipient)
	 * @param listNode The node to receive the message
	 * @return The updated RouteNodeList
	 */
	public RouteNodeList updateRouteNodeList(RouteNodeAbstraction listNode) {
		if (!isNodeinArrayList(listNode)) {
			this.add(listNode);
		}
		return this;
	}

	/**
	 * Updates the RouteNodeList by adding listNode2 to the forwardNodeList of listNode1
	 * @param listNode1 The RouteNodeAbstraction of the relay node
	 * @param listNode2 The RouteNodeAbstraction of the node to further receive the message
	 * @return The updated RouteNodeList
	 */
	public RouteNodeList updateRouteNodeList(RouteNodeAbstraction listNode1, RouteNodeAbstraction listNode2) {
		int index = 0;
		if (isNodeinArrayList(listNode1)) {
			index = this.indexOf(listNode1);
			this.get(index).getForwardNodeList().add(listNode2);
		} else {
			for (int i = 0; i < this.size(); i++) {			
                this.get(i).getForwardNodeList().updateRouteNodeList(listNode1, listNode2);
			}
		}
		return this;
	}

	/**
	 * Checks whether a specified node is in the arraylist
	 * @param listNode The node to check whether or not it is in the RouteNodeList
	 * @return true if the specified node is within the RouteNodeList, false if not.
	 */
	public boolean isNodeinArrayList(RouteNodeAbstraction listNode) {
		boolean found = false;
		for (int i = 0; i < this.size(); i++) {
			if (this.get(i) == listNode) {
				found = true;
				break;
			}
		}
		return found;
	}

	/**
	 * Checks whether a node with the specified ID is in the arraylist
	 * @param nodeId The ID of the node to be checked
	 * @return true if the node with the specified ID is an element of the RouteNodeList, false if not.
	 */
	public boolean isNodeinArrayList(int nodeId) {
		boolean found = false;
		for (int i = 0; i < this.size(); i++) {
			if (this.get(i).getNodeId() == nodeId) {
				found = true;
				break;
			}
		}
		return found;
	}

    /**
     * Returns the sub arraylist of a node with a specified ID
     * @param nodeId The ID of the node to be chosen
     * @return the sub arraylist of a node with a specified ID
     */
    public RouteNodeAbstraction getSubRouteNodeList(int nodeId) {
        for (int i = 0; i < this.size(); i++) {
			if (this.get(i).getNodeId() == nodeId) {
				return this.get(i);
			}
		}
        return null;
    }
}
